﻿using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Threading;
using System.Threading.Tasks;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{

    public partial class ViewModelBase
    {
        public Task NormalTask;
        public string[] WA
        {
            get
            {
                return app.WA;
            }
        }
        public DataTable TBKD
        {
            get
            {
                if (!DBSDDAT.ContainsKey("TBKD"))
                {
                    return null;
                }
                else
                {
                    return DBSDDAT["TBKD"];
                }
            }
            set
            {
                DataTable tb = value.Copy();
                tb.TableName = value.TableName;
                DBSDDAT["TBKD"] = tb;
            }
        }
        public DataTable TBPRG
        {
            get
            {
                return app.TBPRG;
            }
            set
            {
                app.TBPRG = value;
            }
        }
        public DataTable TBUR
        {
            get
            {
                return app.TBUR;
            }
            set
            {
                app.TBUR = value;
            }
        } 
        public Dictionary<string, DataTable> DBSDDAT
        {
            get
            {
                return app.DBSDDAT;
            }

        }
        public Dictionary<string, object> ENVMAIN
        {
            get
            {
                return app.ENVMAIN(app.UserIP);
            }
        }
        public string DBDATE
        {
            get
            {
                string dd = "";
                INTEXDBC.GetDBDATETIME(ref dd, ref DBTIME);
                return dd;
            }
        }
        public ITXConnectInfo itxclient
        {
            get
            { 
                return app.itxclient;
            }
        }
        public string DBTIME = "";
        public string RUNID = "";
        public string date = "";
        public string time = "";
        public string stime = "";
        public string year = "";
        public string month = "";
        public string day = "";
        public DataTable TBTemp;
        public string filter = "";
        public DataRow[] rows;
        public string sort = "";
        public DataRow dwData;
        public DataSet SResult;
        private int _CALL = 0;
        public int CALL
        {
            get { return _CALL; }
            set
            {
                _CALL = value; OnPropertyChanged("CALL", value);
                TBegin = System.DateTime.Now;
            }
        }
        private DateTime _TBegin;
        public DateTime TBegin
        {
            get { return _TBegin; }
            set { _TBegin = value; OnPropertyChanged("TBegin", value); }
        }
        private double _USEC = 0;
        public double USEC
        {
            get { return _USEC; }
            set { _USEC = value; OnPropertyChanged("USEC", value); }
        }
        private double _RSEC = 0;
        public double RSEC
        {
            get { return _RSEC; }
            set { _RSEC = value; OnPropertyChanged("RSEC", value); }
        }
        private int _CNOW = 0;
        public int CNOW
        {
            get { return _CNOW; }
            set
            {
                _CNOW = value; OnPropertyChanged("CNOW", value);
                long elapsedTicks = System.DateTime.Now.Ticks - _TBegin.Ticks;
                TimeSpan elapsedSpan = new TimeSpan(elapsedTicks);
                USEC = elapsedSpan.Minutes * 60 + elapsedSpan.Seconds;
                RSEC = Helpers.Helper.RoundX((_CALL - _CNOW) * _USEC / _CNOW, 1);
            }
        }
        private DataTable _TBMain;
        public DataTable TBMain
        {
            get
            {
                return SetProd.Tables[TableName];
            }
            set
            {
                _TBMain = value;
            }
        }
        private DataTable _TBDetail;
        public DataTable TBDetail
        {
            get
            {
                return SetData.Tables[TableName];
            }
            set
            {
                _TBDetail = value;
            }
        }
        #region======AsyncRun====
        public AsyncRunHandle fHandle
        {
            get;
            set;
        }
        //public readonly TaskScheduler _sync = TaskScheduler.FromCurrentSynchronizationContext();
       
        public delegate DataSet AsyncRunHandle(object p1 = null, object p2 = null, object p3 = null, object p4 = null, object p5 = null);
        public virtual void AsyncRun(AsyncRunHandle fHandle, string TaskType, object p1 = null, object p2 = null, object p3 = null, object p4 = null, object p5 = null)
        {
            try
            {
                AsyncCallback callback = null;
                IAsyncResult aResult = null;
                WaitHandle waitHandle = null;
                callback = new AsyncCallback(this.AsyncCallbackImpl);
                aResult = fHandle.BeginInvoke(p1, p2, p3, p4, p5, callback, fHandle);
                waitHandle = aResult.AsyncWaitHandle;
                waitHandle.WaitOne();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public virtual void AsyncCallbackImpl(IAsyncResult run)
        {
            try
            {
                AsyncRunHandle fHandler = run.AsyncState as AsyncRunHandle;
                DataSet SResult = fHandler.EndInvoke(run);
                Task.Factory.StartNew(() => CallbackImpl(run, SResult));
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public virtual void CallbackImpl(IAsyncResult run, DataSet _SResult)
        {
        }
        #endregion
        public INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public bool IsITX
        {
            get { return app.IsITX; }
            set { app.IsITX = value; }
        }
        private bool _IsOnNew = false; public bool IsOnNew { get { return _IsOnNew; } set { _IsOnNew = value; OnPropertyChanged("IsOnNew", value); } }
        private bool _IsCostMode = false; public bool IsCostMode { get { return _IsCostMode; } set { _IsCostMode = value; OnPropertyChanged("IsCostMode", value); } }
        private bool _IsReport = false; public bool IsReport { get { return _IsReport; } set { _IsReport = value; OnPropertyChanged("IsReport", value); } }
        private bool _IsBottom = false; public bool IsBottom { get { return _IsBottom; } set { _IsBottom = value; OnPropertyChanged("IsBottom", value); } }
        private bool _IsGlobalStart = false; public bool IsGlobalStart { get { return _IsGlobalStart; } set { _IsGlobalStart = value; OnPropertyChanged("IsGlobalStart", value); } }
        private bool _IsBusy = false; public bool IsBusy { get { return _IsBusy; } set { _IsBusy = value; OnPropertyChanged("IsBusy", value); } }
        private bool _IsSaved = false; public bool IsSaved { get { return _IsSaved; } set { _IsSaved = value; OnPropertyChanged("IsSaved", value); } }

        private string _INFO = ""; public string INFO { get { return _INFO; } set { _INFO = value; OnPropertyChanged("INFO", value); } }
        private string _TXT1 = ""; public string TXT1 { get { return _TXT1; } set { _TXT1 = value; OnPropertyChanged("TXT1", value); } }
        private string _TXT2 = ""; public string TXT2 { get { return _TXT2; } set { _TXT2 = value; OnPropertyChanged("TXT2", value); } }
        private string _TXT3 = ""; public string TXT3 { get { return _TXT3; } set { _TXT3 = value; OnPropertyChanged("TXT3", value); } }
        private string _TXT4 = ""; public string TXT4 { get { return _TXT4; } set { _TXT4 = value; OnPropertyChanged("TXT4", value); } }
        private string _KEY = ""; public string KEY { get { return _KEY; } set { _KEY = value; OnPropertyChanged("KEY", value); } }
        private string _DTEND = ""; public string DTEND { get { return _DTEND; } set { _DTEND = value; OnPropertyChanged("DTEND", value); } }
        private string _DTSTART = ""; public string DTSTART { get { return _DTSTART; } set { _DTSTART = value; OnPropertyChanged("DTSTART", value); } }
        private string _VALUE = ""; public string VALUE { get { return _VALUE; } set { _VALUE = value; OnPropertyChanged("VALUE", value); } }
        private List<string> _DEFTXT = new List<string>(); public List<string> DEFTXT { get { return _DEFTXT; } set { _DEFTXT = value; OnPropertyChanged("DEFTXT", value); } }
        private string _TXTISIND = ""; public string TXTISIND { get { return _TXTISIND; } set { _TXTISIND = value; OnPropertyChanged("TXTISIND", value); } }
        private string _ARTST = ""; public string ARTST { get { return _ARTST; } set { _ARTST = value; OnPropertyChanged("ARTST", value); } }
        private string _ORDERNUM = ""; public string ORDERNUM { get { return _ORDERNUM; } set { _ORDERNUM = value; OnPropertyChanged("ORDERNUM", value); } }
        
         private string _EXAUF = ""; public string EXAUF { get { return _EXAUF; } set { _EXAUF = value; OnPropertyChanged("EXAUF", value); } } 
        private string _KOLL = ""; public string KOLL { get { return _KOLL; } set { _KOLL = value; OnPropertyChanged("KOLL", value); } }
        private string _DES = ""; public string DES { get { return _DES; } set { _DES = value; OnPropertyChanged("DES", value); } }
        private string _ART = ""; public string ART { get { return _ART; } set { _ART = value; OnPropertyChanged("ART", value); } }
        private string _VAR = ""; public string VAR { get { return _VAR; } set { _VAR = value; OnPropertyChanged("VAR", value); } }
        private string _FARBE = ""; public string FARBE { get { return _FARBE; } set { _FARBE = value; OnPropertyChanged("FARBE", value); } }
        private string _FSIZE = ""; public string FSIZE { get { return _FSIZE; } set { _FSIZE = value; OnPropertyChanged("FSIZE", value); } }
        private string _DESS = ""; public string DESS { get { return _DESS; } set { _DESS = value; OnPropertyChanged("DESS", value); } }
        private string _COLORIT = ""; public string COLORIT { get { return _COLORIT; } set { _COLORIT = value; OnPropertyChanged("COLORIT", value); } }
        private string _FINISH = ""; public string FINISH { get { return _FINISH; } set { _FINISH = value; OnPropertyChanged("FINISH", value); } }
        private string _EXART = ""; public string EXART { get { return _EXART; } set { _EXART = value; OnPropertyChanged("EXART", value); } }
        private string _KDART = ""; public string KDART { get { return _KDART; } set { _KDART = value; OnPropertyChanged("KDART", value); } }
        private string _KRS = ""; public string KRS { get { return _KRS; } set { _KRS = value; OnPropertyChanged("KRS", value); } }
        private string _LFD = ""; public string LFD { get { return _LFD; } set { _LFD = value; OnPropertyChanged("LFD", value); } }
        private string _POS = ""; public string POS { get { return _POS; } set { _POS = value; OnPropertyChanged("POS", value); } }
        private string _UPOS = ""; public string UPOS { get { return _UPOS; } set { _UPOS = value; OnPropertyChanged("UPOS", value); } }

        private string _YYYY = ""; public string YYYY { get { return _YYYY; } set { _YYYY = value; OnPropertyChanged("YYYY", value); } }
        private string _MM = ""; public string MM { get { return _MM; } set { _MM = value; OnPropertyChanged("MM", value); } }
        private string _DD = ""; public string DD { get { return _DD; } set { _DD = value; OnPropertyChanged("DD", value); } }
        private string _LO = ""; public string LO { get { return _LO; } set { _LO = value; OnPropertyChanged("LO", value); } }
        private string _LB = ""; public string LB { get { return _LB; } set { _LB = value; OnPropertyChanged("LB", value); } }
        private string _LP = ""; public string LP { get { return _LP; } set { _LP = value; OnPropertyChanged("LP", value); } }
        private bool _IsEdit = false; public bool IsEdit { get { return _IsEdit; } set { _IsEdit = value; OnPropertyChanged("IsEdit", value); } }
        private bool _IsExtend = false; public bool IsExtend { get { return _IsExtend; } set { _IsExtend = value; OnPropertyChanged("IsExtend", value); } }

        private bool _IsKEY = false; public bool IsKEY { get { return _IsKEY; } set { _IsKEY = value; OnPropertyChanged("IsKEY", value); } }
        private bool _IsManage = false; public bool IsManage { get { return _IsManage; } set { _IsManage = value; OnPropertyChanged("IsManage", value); } }
        private bool _IsNew = false; public bool IsNew { get { return _IsNew; } set { _IsNew = value; OnPropertyChanged("IsNew", value); } }
        public Dictionary<string, object> PM;

        private DataRow _dwMaster;
        public DataRow dwMaster
        {
            get
            {
                if (_dwMaster == null)
                {
                    if (TBMain.Rows.Count == 0)
                        return null;
                    //return TBMain.Rows[0];
                }
                return _dwMaster;
            }
            set
            {
                _dwMaster = value;
                BatchPropertyChange(value);
            }
        }

        public DataRow dwDetail
        {
            get;
            set;
        }
        private object selectedItem = null;
        public object SelectedItem
        {
            get
            {
                return selectedItem;
            }
            set
            {
                selectedItem = value;
                OnPropertyChanged("SelectedItem", value);
            }
        }
        public string dbConnection = "C:/IntexReport/Data/YGRCPGB04/YGRCPGB04.s3db";
        public SQLiteDatabase SQLiteDatabase;
        SqlConnection _SqlServerDB;
        public SqlConnection SqlServerDB
        {
            get
            {
                if (_SqlServerDB == null)
                    _SqlServerDB = new SqlConnection(app.SqlDB);
                return _SqlServerDB;
            }
        }
    }

}
